<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
    <style>
        .things {
            border: 1px solid #999;
            border-radius: 5px;
            width: 300px;
            margin-top: 10px;
            position: relative;
        }

        .things>li {
            margin-top: 5px;
        }

        .del {
            /* position: absolute;
            right: 100px; */
        }

        .succ {
            /* position: absolute;
            right: 148px; */
        }

        .spanname {
            text-decoration: line-through;
        }

        .res {
            color: green;
        }

        .rej {
            color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <h1>{{title}}</h1>
        <input placeholder="填写事务" v-model="newTask">
        <button @click="add">添加事务</button>
        <ul class="things">
            <li v-for="(obj,index) in list" :key="index">
                <span v-if="obj.state"><del>{{obj.task}}</del></span>
                <span v-else>{{obj.task}}</span>
                <button class="succ" @click="succ(index)">完成</button>
                <button class="del" @click="list.splice(index,1)">删除</button>
            </li>
            <p><b>记事本【已完成：<i class="res">{{cfin}}</i>，未完成：<i class="rej">{{cunfin}}</i>】</b></p>
        </ul>
    </div>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                title: "记事本",
                list: [
                    { task: "睡觉", state: false },
                    { task: "做作业", state: false },
                    { task: "看书", state: false },
                    { task: "听音乐", state: false },
                ],
                newTask: "",
            },
            methods: {
                add() {
                    let obj = {
                        task: this.newTask,
                        state: false
                    }
                    this.list.unshift(obj)
                },
                succ(index) {
                    this.list[index].state = true
                },

            },
            computed: {
                cfin() {
                    var finArr = this.list.filter(function (ele) {
                        return ele.state == true;
                    })
                    return finArr.length;
                },
                cunfin() {
                    var unfinArr = this.list.filter(function (ele) {
                        return ele.state != true;
                    })
                    return unfinArr.length;
                }
            }
        })
    </script>
</body>

</html>